package com.org.android.diary.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.org.android.diary.R;
import com.org.android.diary.databinding.ItemImageBinding;
import com.org.android.diary.utils.glideutil.GlideUtil;
import com.org.android.diary.utils.listener.OnItemClickListener;
import java.util.List;

public class ImageAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<String> strings;
    private Context mcontext;
    OnItemClickListener listener;

    public ImageAdapter(Context context, List<String> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemImageBinding functionBinding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_image, parent, false);
        functionBinding.setAdapter(this);
        return new TourViewHolder(functionBinding);
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        TourViewHolder tv = (TourViewHolder) holder;
        GlideUtil.setCircleMethod(mcontext,strings.get(position),tv.ivModule);

        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemClick(holder.itemView,pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemLongClick(holder.itemView,pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<String> {
        ItemImageBinding functionBinding;
        ImageView ivModule;

        public TourViewHolder(ItemImageBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
        }

        @Override
        public void bindData(String data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }


}







